package org.grow.sha.client;

import org.grow.sha.dao.AccountJpa;
import org.grow.sha.entity.Account;
import org.grow.sha.service.LoginService;
import org.grow.sha.service.LogoutService;
import org.grow.sha.service.TaskExeService;
import org.grow.sha.serviceAop.BackMenuException;
import org.grow.sha.serviceAop.ValidateMenuException;
import org.grow.sha.serviceAop.ValidatePosition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ButterFly implements Runnable{
    @Autowired
    private LoginService loginService;
    @Autowired
    private TaskExeService taskExeService;
    @Autowired
    private LogoutService logoutService;

    Logger logger = LoggerFactory.getLogger(ButterFly.class);
    @Autowired
    private AccountJpa accountJpa;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value("${sha.accounts}")
    private Integer[] accounts;
    @Override
    public void run() {
//        String sql = "select * from Account a";
//        String sql = "select * from Account a where  a.accountId > 16";
//        select * from Account where accountId in (13,15,17);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < accounts.length; i++) {
            sb.append((12+accounts[i]));
            if (i<accounts.length-1) sb.append(",");

        }
        String sql = String.format("select * from Account where accountId in ( %s )", sb.toString());
        System.out.println(sql);
        List<Account> accounts = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));
//        List<Account> accounts = accountJpa.findAll();
//        List<Account> accounts = new ArrayList<>(3);
//        System.out.println(accounts);
        for (int i = 0; i <accounts.size() ; i++) {
            try {
                Account a = accounts.get(i);
                loginService.login(a);
                taskExeService.doingtasks(a);
                logoutService.logout(a);
            }catch (UndeclaredThrowableException e){
                Throwable throwable = e.getUndeclaredThrowable();
                if (throwable instanceof BackMenuException){
                    BackMenuException b =(BackMenuException)throwable;
                    logger.warn("来自切面BackMenuException，将跳过本号 "+b.getMessage());
                    continue;
                }
                if (throwable instanceof ValidateMenuException){
                    ValidateMenuException v = (ValidateMenuException) throwable;
                    logger.warn("来自切面ValidateMenuException，将跳过本号 "+v.getMessage());
                    continue;
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
//        ProcessBuilder bash = new ProcessBuilder("bash", "/home/xieweig/IdeaProjects/sha/src/main/resources/easy.sh");
//        Process start = null;
//        try {
//            start = bash.start();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        InputStream inputStream =
//                start.getInputStream();
//        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
//        String k;
//        while (true){
//            try {
//                if (!((k=br.readLine())!=null)) break;
//                System.out.println(k);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        try {
//            start.waitFor();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }
}
